package cfca.seal.util;

import cfca.seal.system.Env;
import cfca.seal.web.vo.DoubleCircleModelVo;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OutPictureOfDoubleCricle
{
  private static final Logger log = LoggerFactory.getLogger("web_sys");
  private static final int CENTERX = 260;
  private static final int CENTERY = 260;
  private static final int CENTERY_MIDDLE = 255;
  private static final int CENTERY_BOTTOM = 200;
  private static final int RADIUS_OUTTER = 240;
  private static final int RADIUS_INNER = 150;

  private static BufferedImage getCirclePng(String topMessage, String topMessageFontSize, String middleMessage, String middleMessageFontSize, String bottomMessage, String bottomMessageFontSize, String fontFamily, String color, String fiveStar, int sealType)
    throws FileNotFoundException, IOException
  {
    BufferedImage image = null;

    String path = null;
    if (StringUtil.isNotEmpty(fiveStar)) {
      if ("blue".equals(color))
        path = Env.sealResourceDir + File.separator + "starmodel_blue.png";
      else if ("red".equals(color)) {
        path = Env.sealResourceDir + File.separator + "starmodel.png";
      }
    }
    else if ("blue".equals(color))
      path = Env.sealResourceDir + File.separator + "model_blue.png";
    else if ("red".equals(color)) {
      path = Env.sealResourceDir + File.separator + "model.png";
    }

    image = ImageIO.read(new FileInputStream(path));
    Graphics2D g2 = image.createGraphics();
    g2.setFont(new Font(fontFamily, 1, Integer.valueOf(topMessageFontSize).intValue()));

    Color g2Color = new Color(Integer.parseInt("DE", 16), Integer.parseInt("00", 16), Integer.parseInt("0B", 16));
    g2.setColor(g2Color);
    if ("blue".equals(color)) {
      g2.setColor(new Color(Integer.parseInt("3A", 16), Integer.parseInt("69", 16), Integer.parseInt("D5", 16)));
    }
    if (StringUtil.isNotEmpty(topMessage)) {
      drawMessageForUp(g2, topMessage, topMessageFontSize, fontFamily, 260, 260, 240);
    }
    if (middleMessage.split(",").length > 0) {
      middleMessage = middleMessage.split(",")[0];
      Font f = new Font(fontFamily, 1, Integer.valueOf(middleMessageFontSize).intValue());
      g2.setFont(f);
      FontRenderContext context = g2.getFontRenderContext();
      Rectangle2D bounds = f.getStringBounds(middleMessage, context);

      int length = middleMessage.length();
      int char_interval = (int)(bounds.getWidth() / length);
      g2.drawString(middleMessage, 260 - char_interval * length / 2, 365);
    }

    if (StringUtil.isNotEmpty(bottomMessage)) {
      drawMessageForBottom(g2, bottomMessage, bottomMessageFontSize, fontFamily, 260, 200, 240);
    }
    g2.dispose();
    return image;
  }

  private static BufferedImage getDoubleCirclePng(String topMessage, String topMessageFontSize, String middleMessage, String middleMessageFontSize, String bottomMessage, String bottomMessageFontSize, String fontFamily, String color, String fiveStar, int sealType)
    throws FileNotFoundException, IOException
  {
    BufferedImage image = null;

    String path = null;
    if (StringUtil.isNotEmpty(fiveStar)) {
      if ((3 == sealType) && ("blue".equals(color)))
        path = Env.sealResourceDir + File.separator + "starmodel1_blue.png";
      else if ((3 == sealType) && ("red".equals(color))) {
        path = Env.sealResourceDir + File.separator + "starmodel1.png";
      }
    }
    else if ((3 == sealType) && ("blue".equals(color)))
      path = Env.sealResourceDir + File.separator + "model1_blue.png";
    else if ((3 == sealType) && ("red".equals(color))) {
      path = Env.sealResourceDir + File.separator + "model1.png";
    }

    image = ImageIO.read(new FileInputStream(path));
    Graphics2D g2 = image.createGraphics();
    g2.setFont(new Font(fontFamily, 1, Integer.valueOf(topMessageFontSize).intValue()));

    Color g2Color = new Color(Integer.parseInt("DE", 16), Integer.parseInt("00", 16), Integer.parseInt("0B", 16));
    g2.setColor(g2Color);
    if ("blue".equals(color)) {
      g2.setColor(new Color(Integer.parseInt("3A", 16), Integer.parseInt("69", 16), Integer.parseInt("D5", 16)));
    }
    if (StringUtil.isNotEmpty(topMessage)) {
      drawMessageForUp(g2, topMessage, topMessageFontSize, fontFamily, 260, 260, 240);
    }
    if (StringUtil.isNotEmpty(middleMessage)) {
      drawMessageForMiddel(g2, middleMessage, middleMessageFontSize, fiveStar, fontFamily);
    }
    if (StringUtil.isNotEmpty(bottomMessage)) {
      drawMessageForBottom(g2, bottomMessage, bottomMessageFontSize, fontFamily, 260, 200, 240);
    }
    g2.dispose();

    return image;
  }

  private static void drawMessageForMiddel(Graphics2D g2, String middleMessage, String fontSize, String fiveStar, String fontFamily)
  {
    if (StringUtil.isNotEmpty(fiveStar))
    {
      drawMessageForUp(g2, middleMessage.substring(0, middleMessage.length() - 1), fontSize, fontFamily, 260, 255, 150);
    }
    else if (StringUtil.isNotEmpty(middleMessage.replace(",", ""))) {
      Font font = new Font(fontFamily, 1, Integer.valueOf(fontSize).intValue());
      g2.setFont(font);
      String[] message = middleMessage.split(",");

      FontRenderContext context = g2.getFontRenderContext();
      Rectangle2D bounds = font.getStringBounds(middleMessage, context);

      int length = 0; int char_interval = 0;
      if (message.length == 1) {
        length = message[0].length();
        bounds = font.getStringBounds(message[0], context);
        char_interval = (int)(bounds.getWidth() / length);
        g2.drawString(message[0], 260 - char_interval * length / 2, 260);
      }

      if (message.length == 2) {
        length = message[0].length();
        bounds = font.getStringBounds(message[0], context);
        char_interval = (int)(bounds.getWidth() / length);
        g2.drawString(message[0], 260 - char_interval * length / 2, 240);

        int length2 = message[1].length();
        bounds = font.getStringBounds(message[1], context);
        int char_interval2 = (int)(bounds.getWidth() / length2);
        g2.drawString(message[1], 260 - char_interval2 * length2 / 2, 310);
      }
    }
  }

  private static void drawMessageForBottom(Graphics2D g2, String message, String messageFont, String fontSize, int centeRx, int centeRy, int radius)
  {
    String[] messages2 = message.split("", 0);
    String[] messages = new String[messages2.length - 1];
    System.arraycopy(messages2, 1, messages, 0, messages2.length - 1);

    int ilength = messages.length;

    Font f = new Font(fontSize, 1, Integer.valueOf(messageFont).intValue());
    FontRenderContext context = g2.getFontRenderContext();
    Rectangle2D bounds = f.getStringBounds(message, context);

    double char_interval = bounds.getWidth() / ilength;

    double ascent = -bounds.getY();
    int first = 0; int second = 0;
    boolean odd = false;
    if (ilength % 2 == 1) {
      first = (ilength - 1) / 2;
      odd = true;
    } else {
      first = ilength / 2 - 1;
      second = ilength / 2;
      odd = false;
    }
    double radius2 = radius - ascent;
    double x0 = centeRx;
    double y0 = centeRy + radius + ascent;

    double a = 2.0D * Math.asin(char_interval / (2.0D * radius2));
    if (odd) {
      g2.setFont(f);
      g2.drawString(messages[first], (float)(x0 - char_interval / 2.0D), (float)y0);

      for (int i = first + 1; i < ilength; i++) {
        double aa = (i - first) * a;
        double ax = radius2 * Math.sin(aa);
        double ay = radius2 - radius2 * Math.cos(aa);
        AffineTransform transform = AffineTransform.getRotateInstance(-aa);
        Font f2 = f.deriveFont(transform);
        g2.setFont(f2);
        g2.drawString(messages[i], (float)(x0 + ax - char_interval / 2.0D * Math.cos(aa)), (float)(y0 - ay + char_interval / 2.0D * Math.sin(aa)));
      }

      for (int i = first - 1; i > -1; i--) {
        double aa = (first - i) * a;
        double ax = radius2 * Math.sin(aa);
        double ay = radius2 - radius2 * Math.cos(aa);
        AffineTransform transform = AffineTransform.getRotateInstance(aa);
        Font f2 = f.deriveFont(transform);
        g2.setFont(f2);
        g2.drawString(messages[i], (float)(x0 - ax - char_interval / 2.0D * Math.cos(aa)), (float)(y0 - ay - char_interval / 2.0D * Math.sin(aa)));
      }
    }
    else {
      for (int i = second; i < ilength; i++) {
        double aa = (i - second + 0.5D) * a;
        double ax = radius2 * Math.sin(aa);
        double ay = radius2 - radius2 * Math.cos(aa);
        AffineTransform transform = AffineTransform.getRotateInstance(-aa);
        Font f2 = f.deriveFont(transform);
        g2.setFont(f2);
        g2.drawString(messages[i], (float)(x0 + ax - char_interval / 2.0D * Math.cos(aa)), (float)(y0 - ay + char_interval / 2.0D * Math.sin(aa)));
      }

      for (int i = first; i > -1; i--) {
        double aa = (first - i + 0.5D) * a;
        double ax = radius2 * Math.sin(aa);
        double ay = radius2 - radius2 * Math.cos(aa);
        AffineTransform transform = AffineTransform.getRotateInstance(aa);
        Font f2 = f.deriveFont(transform);
        g2.setFont(f2);
        g2.drawString(messages[i], (float)(x0 - ax - char_interval / 2.0D * Math.cos(aa)), (float)(y0 - ay - char_interval / 2.0D * Math.sin(aa)));
      }
    }
  }

  private static void drawMessageForUp(Graphics2D g2, String message, String fontSize, String messageFont, int centeRx, int centeRy, int radius)
  {
    if (message.split(",").length > 0) {
      message = message.split(",")[0];
    }
    String[] messages2 = message.split("", 0);
    String[] messages = new String[messages2.length - 1];
    System.arraycopy(messages2, 1, messages, 0, messages2.length - 1);

    int ilength = messages.length;

    Font f = new Font(messageFont, 1, Integer.valueOf(fontSize).intValue());
    FontRenderContext context = g2.getFontRenderContext();
    Rectangle2D bounds = f.getStringBounds(message, context);

    double char_interval = bounds.getWidth() / ilength;

    double ascent = -bounds.getY();
    int first = 0; int second = 0;
    boolean odd = false;
    if (ilength % 2 == 1) {
      first = (ilength - 1) / 2;
      odd = true;
    } else {
      first = ilength / 2 - 1;
      second = ilength / 2;
      odd = false;
    }
    double radius2 = radius - ascent;
    double x0 = centeRx;
    double y0 = centeRy - radius + ascent;

    double a = 2.0D * Math.asin(char_interval / (2.0D * radius2));
    if (odd) {
      g2.setFont(f);
      g2.drawString(messages[first], (float)(x0 - char_interval / 2.0D), (float)y0);

      for (int i = first + 1; i < ilength; i++) {
        double aa = (i - first) * a;
        double ax = radius2 * Math.sin(aa);
        double ay = radius2 - radius2 * Math.cos(aa);
        AffineTransform transform = AffineTransform.getRotateInstance(aa);
        Font f2 = f.deriveFont(transform);
        g2.setFont(f2);
        g2.drawString(messages[i], (float)(x0 + ax - char_interval / 2.0D * Math.cos(aa)), (float)(y0 + ay - char_interval / 2.0D * Math.sin(aa)));
      }

      for (int i = first - 1; i > -1; i--) {
        double aa = (first - i) * a;
        double ax = radius2 * Math.sin(aa);
        double ay = radius2 - radius2 * Math.cos(aa);
        AffineTransform transform = AffineTransform.getRotateInstance(-aa);
        Font f2 = f.deriveFont(transform);
        g2.setFont(f2);
        g2.drawString(messages[i], (float)(x0 - ax - char_interval / 2.0D * Math.cos(aa)), (float)(y0 + ay + char_interval / 2.0D * Math.sin(aa)));
      }
    }
    else {
      for (int i = second; i < ilength; i++) {
        double aa = (i - second + 0.5D) * a;
        double ax = radius2 * Math.sin(aa);
        double ay = radius2 - radius2 * Math.cos(aa);
        AffineTransform transform = AffineTransform.getRotateInstance(aa);
        Font f2 = f.deriveFont(transform);
        g2.setFont(f2);
        g2.drawString(messages[i], (float)(x0 + ax - char_interval / 2.0D * Math.cos(aa)), (float)(y0 + ay - char_interval / 2.0D * Math.sin(aa)));
      }

      for (int i = first; i > -1; i--) {
        double aa = (first - i + 0.5D) * a;
        double ax = radius2 * Math.sin(aa);
        double ay = radius2 - radius2 * Math.cos(aa);
        AffineTransform transform = AffineTransform.getRotateInstance(-aa);
        Font f2 = f.deriveFont(transform);
        g2.setFont(f2);
        g2.drawString(messages[i], (float)(x0 - ax - char_interval / 2.0D * Math.cos(aa)), (float)(y0 + ay + char_interval / 2.0D * Math.sin(aa)));
      }
    }
  }

  public static byte[] getCirclePngToByte(DoubleCircleModelVo sealModelVo)
  {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    BufferedImage buffImg = null;
    try
    {
      buffImg = getCirclePng(sealModelVo.getTopMessage(), sealModelVo.getTopMessageFontSize(), sealModelVo.getMiddleMessage(), sealModelVo.getMiddleMessageFontSize(), sealModelVo.getBottomMessage(), sealModelVo.getBottomMessageFontSize(), sealModelVo.getFontFamily(), sealModelVo.getModelColor(), sealModelVo.getFiveStar(), sealModelVo.getSealType());

      ImageIO.write(buffImg, "png", byteArrayOutputStream);
    }
    catch (IOException e) {
      e.printStackTrace();
      log.error("生成圆形图片出错", e);
    }
    return ImageUtil.resizeImage(byteArrayOutputStream.toByteArray(), sealModelVo.getImageWidth(), sealModelVo.getImageHeight());
  }

  public static byte[] getDoubleCirclePngToByte(DoubleCircleModelVo sealModelVo)
  {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    BufferedImage buffImg = null;
    try
    {
      buffImg = getDoubleCirclePng(sealModelVo.getTopMessage(), sealModelVo.getTopMessageFontSize(), sealModelVo.getMiddleMessage(), sealModelVo.getMiddleMessageFontSize(), sealModelVo.getBottomMessage(), sealModelVo.getBottomMessageFontSize(), sealModelVo.getFontFamily(), sealModelVo.getModelColor(), sealModelVo.getFiveStar(), sealModelVo.getSealType());

      ImageIO.write(buffImg, "png", byteArrayOutputStream);
    }
    catch (IOException e) {
      e.printStackTrace();
      log.error("生成圆形图片出错", e);
    }
    return ImageUtil.resizeImage(byteArrayOutputStream.toByteArray(), sealModelVo.getImageWidth(), sealModelVo.getImageHeight());
  }
}